//
//  NSObject+DeleteNull.m
//  
//
//  Created by  on 16/7/27.
//  Copyright © 2016年 . All rights reserved.
//

#import "NSObject+DeleteNull.h"

@implementation NSObject (DeleteNull)
#pragma mark - 私有方法
//将NSDictionary中的Null类型的项目转化成nil
-(NSDictionary *)nullDic
{
    NSDictionary *myDic = (NSDictionary*)self;
    NSArray *keyArr = [myDic allKeys];
    NSMutableDictionary *resDic = [NSMutableDictionary dictionaryWithDictionary:myDic];
    for (int i = 0; i < keyArr.count; i ++)
    {
        id obj = [myDic objectForKey:keyArr[i]];
        
        if([obj isKindOfClass:[NSNull class]]){
            [resDic removeObjectForKey:keyArr[i]];
        }else{
            obj = [obj deleteNull];
        }
    }
    return resDic;
}

//将NSArray中的Null类型的删除
-(NSArray *)nullArr
{
    NSArray *myArr = (NSArray*)self;
    NSMutableArray *resArr = [NSMutableArray arrayWithArray:myArr];
    for (int i = 0; i < myArr.count; i ++)
    {
        id obj = myArr[i];
        
        if([obj isKindOfClass:[NSNull class]]){
            [resArr removeObject:obj];
        }else{
            obj = [obj deleteNull];
        }
    }
    return resArr;
}

//将Null类型的项目转化成nil
-(NSString *)nullToString
{
    return nil;
}

#pragma mark - 公有方法
//类型识别:将所有的NSNull类型转化成nil
-(id)deleteNull
{
    if ([self isKindOfClass:[NSDictionary class]]){
        return [self nullDic];
        
    }else if([self isKindOfClass:[NSArray class]])
    {
        return [self nullArr];
        
    }else if([self isKindOfClass:[NSNull class]]){
        return [self nullToString];
        
    }else
    {
        return self;
    }
}
@end
